package pers.qianyu.module.logging.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.core.comm.*;
import pers.qianyu.module.core.domain.logging.query.*;
import pers.qianyu.module.core.domain.logging.vo.*;
import pers.qianyu.module.logging.service.*;

import javax.validation.constraints.*;

/**
 * @author mizzle rain
 * @date 2020-10-30 15:03
 */
@Api(tags = "日志相关接口")
@Validated
@RestController
@RequestMapping("/logging-serv")
public class RequestLogController {
    @Autowired
    private RequestLogService requestLogService;
    @Autowired
    private ExcelExportService excelExportService;

    // 日志直接从内部插入，不提供外部插入接口
    //@PostMapping("/request_log")
    //public HttpResult<?> insert(@RequestBody RequestLogDTO requestLogDTO) {
    //    requestLogService.insert(requestLogDTO);
    //    return HttpResult.okWithMessage("日志插入成功！");
    //}

    @ApiOperation("分页查询日志信息")
    @GetMapping("/request_log")
    public HttpResult<Pagination<RequestLogVO>> query(RequestLogQuery requestLogQuery) {
        Pagination<RequestLogVO> page = requestLogService.query(requestLogQuery);
        return HttpResult.okWithData(page);
    }

    @ApiOperation("导出日志信息为Excel表格")
    @GetMapping("/export")
    public HttpResult<String> export(RequestLogQuery requestLogQuery,
                                     @NotEmpty(message = "文件名不能为空") String fileName) {
        // excelExportService.export(requestLogQuery, fileName);
        excelExportService.asyncExport(requestLogQuery, fileName);
        return HttpResult.okWithData("/uploads/" + fileName);
    }
}
